//帮助中心
define(['jquery', 'typed', 'jqueryvalidata', 'jqueryUI'], function ($, typed, jqueryvalidata, jqueryUI) {
    var appCenter = {};
    appCenter.init = function () {
        //console.log("appCenter");


        //打字效果
        $(".typedH2").typed({
            strings: ["不随大众，我们服务于高要求 , 高品质的客户 !", "不断追求创新，只为政务服务！"],
            typeSpeed: 100,
            backDelay: 500,
            loop: true,
        });


        //日期控件
        var start = {
            elem: '#complaintudatetime',
            format: 'YYYY-MM-DD',
            max: laydate.now(), //最大日期
            istoday: true
        };
        $("#complaintudatetime").click(function () {
            laydate(start);
        });

        //注册文本框最大值控制
        textMaxLength();


        //下载分类
        //如果是表单下载模块，菜单不重新定位
        console.log($('#hidCurrentMenuId'));
        if ($('#hidCurrentMenuId') != null) {
            console.log($('#hidCurrentMenuId').val());
            if ($('#hidCurrentMenuId').val() != "index_5") {

                //$(".download-category dd").click(function () {
                //    $(this).addClass("active").siblings().removeClass("active");
                //});

            }
        }
        else {
            //$(".download-category dd").click(function () {
            //    $(this).addClass("active").siblings().removeClass("active");
            //});
        }


        //注册上传事件
        $('#btnuploadfile').click(function () {
            applyfileupload('fileUpload');
        });

        validateInit();

        $(".report-tab-hd > li:first").addClass("active");
        $(".report-tab-bd >div").not(":first").hide();
        $(".report-tab-hd > li").unbind("click").bind("click", function () {

            $(this).siblings().removeClass("active").end().addClass("active");
            var index = $(".report-tab-hd > li").index($(this));
            $(".report-tab-bd > div").eq(index).siblings(".report-tab-bd >div").hide().end().fadeIn("slow");
            //console.log("index:" + index);
            //validata1();

            $('#cctype').val(index);

            if (index == 0) {
                validateCcUser();
            }
            else {
                validateCcUnit();
            }


        });


    }


    function validateInit() {
        $("#inputForm").validate();
        validateMain();
        validateCcUser();
    }

    function validateMain() {

        //举报人电子邮件
        $("#complaintemail").rules("remove");
        $("#complaintemail").rules("add", {
            required: true,
            email: true,
            messages: {
                required: "请填写您的电子邮件",
                email: "您输入的邮件格式不正确"
            }
        });

        //举报内容-事项名称
        $("#complainttitle").rules("remove");
        $("#complainttitle").rules("add", {required: true, messages: {required: "请填写被举报事项名称"}});

        //举报内容-举报事项发生日期
        $("#complaintudatetime").rules("remove");
        $("#complaintudatetime").rules("add", {required: true, messages: {required: "请选择举报事项发生日期"}});

        //举报内容-举报事项内容
        $("#complaintcontent").rules("remove");
        $("#complaintcontent").rules("add", {required: true, messages: {required: "请填写举报事项内容"}});
    }

    function validateCcUser() {

        //被举报人姓名
        $("#becomplaint1username").rules("remove");
        $("#becomplaint1username").rules("add", {required: true, messages: {required: "请填写被举报人姓名"}});

        //被举报人单位
        $("#becomplaint1unitname").rules("remove");
        $("#becomplaint1unitname").rules("add", {required: true, messages: {required: "请填写被举报人单位"}});

        //被举报人-举报发生时间
        $("#becomplaint1period").rules("remove");
        $("#becomplaint1period").rules("add", {required: true, messages: {required: "请选择举报发生时间"}});

        //被举报人-警种
        $("#becomplaint1policetype").rules("remove");
        $("#becomplaint1policetype").rules("add", {required: true, messages: {required: "请选择警种"}});
    }

    function validateCcUnit() {

        //被举报单位-名称
        $("#becomplaint2unitname").rules("remove");
        $("#becomplaint2unitname").rules("add", {required: true, messages: {required: "请填写被举报单位"}});

        //被举报单位-举报发生时间
        $("#becomplaint2period").rules("remove");
        $("#becomplaint2period").rules("add", {required: true, messages: {required: "请选择举报发生时间"}});

        //被举报单位-警种
        $("#becomplaint2policetype").rules("remove");
        $("#becomplaint2policetype").rules("add", {required: true, messages: {required: "请选择警种"}});

    }


    function textMaxLength() {
        //调用
        statInputNum($('#complaintcontent'), $('#complaintcontent_count'));
    }

    function checkRN(text) {
        var reg = /[\r\n\S]+/g;
        //console.log(reg.test(text));
        return reg.test(text);
    }

    /*
     * 剩余字数统计
     * 注意 最大字数只需要在放数字的节点哪里直接写好即可 如：<var class="word">200</var>
     */
    function statInputNum(textArea, numItem) {
        var max = numItem.text(),
            curLength;
        if (textArea[0] != null) {
            //textArea[0].setAttribute("maxlength", max);
            curLength = textArea.val().length;
            numItem.text(max - curLength);
            textArea.on('input propertychange', function () {
                //console.log("s:" + $(this).val().length);
                //var _value = $(this).val().replace(/\n/gi, "");

                //console.log("e:" + _value.length);
                //numItem.text(max - _value.length);
                //console.log(checkRN($(this).val()));
                //var i = 0;
                //if (checkRN($(this).val())) {
                //    console.log("有换行");
                //    i = 1;
                //}
                //console.log("e:" + $(this).val().length + "|" + i);
                if($(this).val().length >= max){
                    $(this).val($(this).val().substr(0,max));
                }
                numItem.text(max - $(this).val().length);
            });
        }
    }

    // 中文字符判断
    function getStrLength(str) {
        var len = str.length;
        var reLen = 0;
        for (var i = 0; i < len; i++) {

            if (str.charCodeAt(i) < 27 || str.charCodeAt(i) > 126) {
                // 全角
                reLen += 2;
            } else {
                reLen++;
            }
        }
        return reLen;
    }

    return appCenter;
})